// pages/fuelcharging/fuelcharging.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    paymethod: 'bapaymen',
    ispay: !1,
    isnooilpay: !1,
    iswashpay: !1,
    isshow: !0,
    iscon: !1,
    isSuperVip:!1,
    isFocus: false, //控制input 聚焦
    iserr: !1,
    isenough: !1,
    OilGunList: [],
    isopen: !0,
    price: '',
    choosenum: 0,
    chooseid: 0,
    inputFocus: false,
    logininfo: '',
    OilGunList: [],
    stationname: '',
    navsel: '1',
    plate: '京',
    platearr: ["京", "津", "沪", "渝", "蒙", "新", "藏", "宁", "桂", "黑", "吉", "辽", "晋", "翼", "青", "鲁", "豫", "苏", "皖", "浙", "闽", "赣", "湘", "鄂", "粤", "琼", "甘", "陕", "贵", "云", "川", "0",],
    isplate: !1,
    platenum: '',
    inputFocusplatenum: !1,
    washcatelist: [],
    washmycardlist: [],
    washcatedata: "",
    balance: 0,
    last: '', isshowhis: !1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    let logininfo = app.logininfo,
      stationname = '',
      page = getCurrentPages();
    page = page[page.length - 2];
    console.log(page, 'pagepagepage');
    let { refulinglist } = page.data
    console.log(refulinglist, 'refulinglistrefulinglist');
    refulinglist.forEach(x => {
      if (x.id == options.id) {
        stationname = x.name
      }
    })
    let userInfo = wx.getStorageSync('userInfo')
    that.setData({
      stationId: options.id,
      logininfo,
      stationname, userInfo
    }, () => {
      that.getdate()
    })

  },
  getdate() {
    let {
      stationId
    } = that.data
    console.log(stationId, 'stationIdstationIdstationId');

    let { corpGoodsTag, corpWashTag ,msgTplIdArr} = app.logininfo
    that.setData({ corpGoodsTag, corpWashTag ,msgTplIdArr})
    api.oildevice(stationId, res => {
      if (res.data.code == 0) {
        if (res.data.entity) {
          that.setData({
            OilGunList: res.data.entity.data
          })
        }
      }
    })
    api.washcate(stationId, res => {
      if (res.data.code == 0) {
        if (res.data.entity) {
          let washcatedata = res.data.entity.data.infoArr[0]
          that.setData({ washcatelist: res.data.entity.data, washcatedata, washcateid: washcatedata.id })
        }
      }
    })
    that.washmycard()
  },
  washmycard() {
    let {
      stationId
    } = that.data, historycarnum = []
    api.washmycard(stationId, res => {
      if (res.data.code == 0) {
        if (res.data.entity) {
          let washmycardlist = res.data.entity.data
          washmycardlist.infoArr.forEach(x => {
            x.payTime = util.toDate1(x.payTime)
            x.validTime = util.toDate1(x.validTime)
          })
          that.setData({
            washmycardlist
          })
        }
      }
    })
    let history = wx.getStorageSync('historycarnum')
    if (history) {
      historycarnum = history
    }
    that.setData({ historycarnum })
  },
  clickinput() {
    console.log(that.data.inputFocus, 'inputFocusinputFocus');

    that.setData({
      inputFocus: !0,
      isshow: !1
    })
  },
  inputvalue(e) {
    that.setData({ last: e.detail.value })
  },
  clickinputplatenum() {
    let {
      plate
    } = that.data
    if (plate != '0') {
      that.setData({
        inputFocusplatenum: !0, isshowhis: !0
      })
    }
  },
  clickislastFocus() {
    that.setData({
      islastFocus: !0
    })
  },
  bindblur() {
    that.setData({
      isshow: !0,
      inputFocus: !1,
      inputFocusplatenum: !1,
      islastFocus: !1
    })
  },
  inputtap(e) {
    let {
      value
    } = e.detail
    that.setData({
      price: value
    })
  },
  inputtapplatenum(e) {
    let {
      value
    } = e.detail
    that.setData({
      platenum: value.toUpperCase()
    })
  },
  open() {
    let {
      isopen
    } = that.data
    that.setData({
      isopen: !isopen
    })
  },
  selnum(e) {
    let { item } = e.currentTarget.dataset, { last } = that.data
    that.setData({ last: item.last, platenum: item.platenum, plate: item.plate, isshowhis: !1 })
  },
  tochoose(e) {
    let {
      num
    } = e.currentTarget.dataset, {
      OilGunList,
      choosenum,
      chooseid
    } = that.data
    OilGunList.forEach(x => {
      if (x.id == num) {
        choosenum = parseInt(x.name)
        chooseid = x.id
      }
    })
    that.setData({
      choosenum,
      chooseid
    })
  },
  //确认加油
  confirmRefueling() {
    let {
      choosenum,
      price,
      chooseid
    } = that.data, err = ''
    if (price <= 0 || price == '') err = "请输入金额"
    if (chooseid == 0) err = "请选择油枪"
    if (err != '') {
      wx.showModal({
        content: err,
        showCancel: !1,
      })
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    api.oilplace({
      oilFee: price,
      did: chooseid,
      fromId:0,
    }, res => {
      wx.hideLoading()
      if (res.data.code == 0) {
        let {
          couponFee,
          levelName,
          litPic,
          name,
          needPay,
          orderId,
          oilName,
          hasPayPwd
        } = res.data.entity.data

        that.setData({
          ispay: !0,
          isshow: !1,
          couponFee,
          levelName,
          litPic,
          name,
          needPay,
          orderId,
          oilName,
          hasPayPwd
        })

      }
    })
  },
  // 非油品 确认加油
  confirmRefueling1() {
    let {
      stationId,
      price
    } = that.data, err = ''
    if (price <= 0 || price == '') err = "请输入金额"
    // if(chooseid==0)err="请选择油枪"
    if (err != '') {
      wx.showModal({
        content: err,
        showCancel: !1,
      })
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    api.goodsplace({
      fee: price,
      stationId
    }, res => {
      wx.hideLoading()
      if (res.data.code == 0) {
        let {
          couponFee,
          levelName,
          litPic,
          name,
          needPay,
          orderId,
          oilName,
          hasPayPwd
        } = res.data.entity.data

        that.setData({
          isnooilpay: !0,
          isshow: !1,
          couponFee,
          levelName,
          litPic,
          name,
          needPay,
          orderId,
          oilName,
          hasPayPwd
        })

      }
    })
  },
  //非油品 支付
  toconfirm1() {
    let { paymethod, orderId, hasPayPwd, navsel } = that.data
    if (paymethod == 'bapaymen') { //余额支付
      if (hasPayPwd == 0) {
        that.setData({
          isenough: !0,
          isshow: !1,
          isFocus: !1,
          iscon: !1
        })
      } else {
        that.setData({
          iscon: !0,
          ispay: !1,
          isFocus: true,
          wallets_password: '',
          isnooilpay: !1, balance: 1,
        })
      }
    } else { //微信支付
      wx.showLoading({
        title: '加载中',
      })
      api.systemcorpinfo(res => {
        if (res.data.code == 0) {
          if (res.data.entity.data.corpExpired == 1) {
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: '系统已过期，无法充值',
              showCancel: !1,
            })
            return;
          } else {
            api.nooilprepay({
              orderId
            }, res => {
              wx.hideLoading()
              console.log(res, 'res');
              if (res.succ == 1) {
                wx.navigateTo({
                  url: '../orderpaysucc/orderpaysucc?orderId=' + orderId + '&code=' + navsel,
                })
              } else {
                wx.showModal({
                  content: res.msg,
                  showCancel: !1,
                })
              }
            })
          }
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      })
    }
  },
  // 洗车
  confirmRefueling2() {
    // wx.navigateTo({
    //   url: `../buysucc/buysucc`,
    // })
    let { platenum, washcateid, stationId, plate, last } = that.data, err = '', oldplatenum = platenum
    if (plate != '0' && platenum.length < 6) err = "请输入完整的牌号"
    if (platenum == '') err = "请输入牌号"
    if (err != '') {
      wx.showModal({
        content: err,
        showCancel: !1,
      })
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    if (last != '') {

      platenum = platenum + last
    }
    let carPlate = ''
    if (plate == '0') {
      carPlate = platenum
    } else {
      carPlate = plate + platenum
    }
    api.washplace({ cateId: washcateid, carPlate, stationId }, res => {
      wx.hideLoading()
      if (res.data.code == 0) {
        if (plate != '0') {
          let history = wx.getStorageSync('historycarnum')
          if (history.length < 3) {
            let data = { platenum: oldplatenum, plate: plate, last: last }, list = []
            list.push(data)
            if (history.length > 0) {
              history.forEach(x => {
                if (x.platenum == oldplatenum && x.plate == plate && x.last == last) {
                  return
                } else {
                  let newlist = list.concat(history)
                  wx.setStorageSync('historycarnum', newlist)
                }
              })

            } else {
              let newlist = list
              wx.setStorageSync('historycarnum', newlist)
            }
          } else {
            let history = wx.getStorageSync('historycarnum'), list = []
            history.splice(0, 1)
            let data = { platenum: oldplatenum, plate: plate, last: last }
            list.push(data)
            let newlist = list.concat(history)
            wx.setStorageSync('historycarnum', newlist)
          }
        }
        let { couponFee, levelName, litPic, name, needPay, orderId, oilName, hasPayPwd } = res.data.entity.data
        that.setData({ iswashpay: !0, isshow: !1, couponFee, levelName, litPic, name, needPay, orderId, oilName, hasPayPwd })
      }
    })
  },
  //洗车 支付
  toconfirm2() {
    let { paymethod, orderId, hasPayPwd, navsel, needPay } = that.data
    if (paymethod == 'bapaymen') { //余额支付
      if (hasPayPwd == 0) {
        that.setData({
          isenough: !0,
          isshow: !1,
          isFocus: !1,
          iscon: !1
        })
      } else {
        that.setData({ iscon: !0, iswashpay: !1, isFocus: true, wallets_password: '', balance: 2, })
      }
    } else { //微信支付
      wx.showLoading({
        title: '加载中',
      })
      api.systemcorpinfo(res => {
        if (res.data.code == 0) {
          if (res.data.entity.data.corpExpired == 1) {
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: '系统已过期，无法充值',
              showCancel: !1,
            })
            return;
          } else {
            if (Number(needPay) > 0) {
              api.washprepay({
                orderId
              }, res => {
                wx.hideLoading()
                console.log(res, 'res');
                if (res.succ == 1) {
                  wx.navigateTo({
                    url: `../buysucc/buysucc?orderId=${orderId}`,
                  })
                  // wx.navigateTo({
                  //   url: '../orderpaysucc/orderpaysucc?orderId=' + orderId + '&code=' + navsel,
                  // })
                } else {
                  wx.showModal({
                    content: res.msg,
                    showCancel: !1,
                  })
                }
              })
            } else {
              let payType = 2
              api.washspecailpay({ orderId, payType }, res => {
                if (res.data.code == 0) {
                  wx.navigateTo({
                    url: `../buysucc/buysucc?orderId=${orderId}`,
                  })
                }else {
                  wx.showModal({
                    content: res.msg,
                    showCancel: !1,
                  })
                }
              })
            }
          }
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      })
    }
  },
  inputFn: function (e) {
    return e.detail.value.toUpperCase()
  },
  close() {
    that.setData({
      ispay: !1,
      isshow: !0,
      isnooilpay: !1, iswashpay: !1,
    })
  },
  tochoosepay(e) {
    let {
      way
    } = e.currentTarget.dataset, {
      paymethod
    } = that.data
    if (way == paymethod) {
      return
    }
    that.setData({
      paymethod: way
    })
  },
  set_wallets_password(e) { //获取钱包密码，余额支付
    // console.log(e,'eeeeeeeeeeeee');
    let {
      orderId,
      navsel, balance, needPay
    } = that.data
    this.setData({
      wallets_password: e.detail.value
    });
    if (this.data.wallets_password.length == 6) { //密码长度6位时，自动验证钱包支付结果
      let pwd = e.detail.value
      if (balance == 0) {//油品余额支付
        api.ordercheckaccountpay({
          orderId,
          pwd
        }, res => {
          if (res.data.code == 0) {
            that.setData({
              isFocus: !1,
            }, () => {
              wx.navigateTo({
                url: '../orderpaysucc/orderpaysucc?orderId=' + orderId + '&code=' + navsel,
              })
            })
          } else if (res.data.code == 20312) {
            that.setData({
              iserr: !0,
              isshow: !1,
              isFocus: !1,
              iscon: !1
            })
          } else {
            that.setData({
              isenough: !0,
              isshow: !1,
              isFocus: !1,
              iscon: !1
            })
          }
        })
      } else if (balance == 1) {//非油品余额支付
        api.goodsaccountpay({
          orderId,
          pwd
        }, res => {
          if (res.data.code == 0) {
            that.setData({
              isFocus: !1,
            }, () => {
              wx.navigateTo({
                url: '../orderpaysucc/orderpaysucc?orderId=' + orderId + '&code=' + navsel,
              })
            })
          } else if (res.data.code == 20312) {
            that.setData({
              iserr: !0,
              isshow: !1,
              isFocus: !1,
              iscon: !1
            })
          } else {
            that.setData({
              isenough: !0,
              isshow: !1,
              isFocus: !1,
              iscon: !1
            })
          }
        })
      } else if (balance == 2) {//洗车余额支付
        if (Number(needPay) > 0) {
          api.washaccountpay({
            orderId,
            pwd
          }, res => {
            if (res.data.code == 0) {
              that.setData({
                isFocus: !1,
              }, () => {
                wx.navigateTo({
                  url: '../buysucc/buysucc?orderId=' + orderId,
                })
              })
            } else if (res.data.code == 20312) {
              that.setData({
                iserr: !0,
                isshow: !1,
                isFocus: !1,
                iscon: !1
              })
            } else {
              that.setData({
                isenough: !0,
                isshow: !1,
                isFocus: !1,
                iscon: !1
              })
            }
          })
        } else {
          let payType = 1
          api.washspecailpay({ orderId, payType }, res => {
            if (res.data.code == 0) {
              wx.navigateTo({
                url: `../buysucc/buysucc?orderId=${orderId}`,
              })
            } else if (res.data.code == 20312) {
              that.setData({
                iserr: !0,
                isshow: !1,
                isFocus: !1,
                iscon: !1
              })
            } else {
              that.setData({
                isenough: !0,
                isshow: !1,
                isFocus: !1,
                iscon: !1
              })
            }
          })
        }
      }
    }
  },
  close_wallets_password() {
    that.setData({
      iscon: !1,
      isshow: !0
    })
  },
  set_Focus() { //聚焦input
    console.log('isFocus', this.data.isFocus)
    this.setData({
      isFocus: true
    })
  },

  toconfirm() {
    let { paymethod, orderId, hasPayPwd, navsel, msgTplIdArr } = that.data, newmsgTplIdArr = []
    newmsgTplIdArr.push(msgTplIdArr.couponGain, msgTplIdArr.couponOverdue, msgTplIdArr.scoreChange)
    wx.requestSubscribeMessage({
      tmplIds: newmsgTplIdArr,
      success: function (res) {
        console.log(res, 'eeeeeeee')
        if (paymethod == 'bapaymen') { //余额支付
          if (hasPayPwd == 0) {
            that.setData({
              isenough: !0,
              isshow: !1,
              isFocus: !1,
              iscon: !1
            })
          } else {
            that.setData({
              iscon: !0,
              ispay: !1,
              isFocus: true,
              wallets_password: '',
              isnooilpay: !1,
              iswashpay: !1,
            })
          }
        } else { //微信支付
          wx.showLoading({
            title: '加载中',
          })
          api.systemcorpinfo(res => {
            if (res.data.code == 0) {
              if (res.data.entity.data.corpExpired == 1) {
                wx.hideLoading()
                wx.showModal({
                  title: '提示',
                  content: '系统已过期，无法充值',
                  showCancel: !1,
                })
                return;
              } else {
                api.prepay({
                  orderId
                }, res => {
                  wx.hideLoading()
                  console.log(res, 'res');
                  if (res.succ == 1) {
                    wx.navigateTo({
                      url: '../orderpaysucc/orderpaysucc?orderId=' + orderId + '&code=' + navsel,
                    })
                  } else {
                    wx.showModal({
                      content: res.msg,
                      showCancel: !1,
                    })
                  }
                })
              }
            } else {
              wx.hideLoading()
              wx.showToast({
                title: res.data.message,
                icon: 'none'
              })
            }
          })
        }
      },
      fail: function (err) {
        console.log(err, 'rrrrrrrrr')
        if (paymethod == 'bapaymen') { //余额支付
          if (hasPayPwd == 0) {
            that.setData({
              isenough: !0,
              isshow: !1,
              isFocus: !1,
              iscon: !1
            })
          } else {
            that.setData({
              iscon: !0,
              ispay: !1,
              isFocus: true,
              wallets_password: '',
              isnooilpay: !1,
              iswashpay: !1,
            })
          }
        } else { //微信支付
          wx.showLoading({
            title: '加载中',
          })
          api.systemcorpinfo(res => {
            if (res.data.code == 0) {
              if (res.data.entity.data.corpExpired == 1) {
                wx.hideLoading()
                wx.showModal({
                  title: '提示',
                  content: '系统已过期，无法充值',
                  showCancel: !1,
                })
                return;
              } else {
                api.prepay({
                  orderId
                }, res => {
                  wx.hideLoading()
                  console.log(res, 'res');
                  if (res.succ == 1) {
                    wx.navigateTo({
                      url: '../orderpaysucc/orderpaysucc?orderId=' + orderId + '&code=' + navsel,
                    })
                  } else {
                    wx.showModal({
                      content: res.msg,
                      showCancel: !1,
                    })
                  }
                })
              }
            } else {
              wx.hideLoading()
              wx.showToast({
                title: res.data.message,
                icon: 'none'
              })
            }
          })
        }
      }
    })

  },

  modify_password() {
    that.setData({
      iserr: !1,
      iscon: !1
    }, () => {
      wx.navigateTo({
        url: '../forgetPwd/forgetPwd',
      })
    })
  },
  retry() {
    that.setData({
      iserr: !1,
      iscon: !0,
      wallets_password: '',
      isFocus: !0,
      wallets_password: ''
    })
  },
  closeerr() {
    that.setData({
      iserr: !1,
      iscon: !1,
      isshow: !0,
      isplate: !1
    })
  },
  torecharge() {
    wx.navigateTo({
      url: '../recharge/recharge',
    })
  },
  closebu() {
    that.setData({
      isenough: !1,
      isshow: !0,
      wallets_password: ''
    })
  },
  navsel(e) {
    let {
      code
    } = e.currentTarget.dataset
    console.log(code, 'codecode');
    that.setData({
      navsel: code,
      price: '',
      paymethod: 'bapaymen'
    })
  },
  platesel() {
    that.setData({
      isplate: !0
    })
  },
  plate(e) {
    let {
      item
    } = e.currentTarget.dataset, {
      plate
    } = that.data
    if (plate == item) {
      return;
    } else {
      if (item == '0') {
        that.setData({
          platenum: '临时号牌'
        })
      } else {
        that.setData({
          platenum: ''
        })
      }
    }

    that.setData({
      plate: item,
      isplate: !1
    })
  },
  //切换洗车类型
  intensivebtn(e) {
    let { id } = e.currentTarget.dataset, { washcatelist } = that.data
    washcatelist.infoArr.forEach(x => {
      if (x.id == id) {
        that.setData({ washcatedata: x, washcateid: x.id })
      }
    })
  },
  to(e) {
    let { url, brief } = e.currentTarget.dataset
    wx.navigateTo({
      url: `../${url}/${url}?brief=${brief}`,
    })
  },
  closeishowhis() {
    that.setData({ isshowhis: !1 })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    that.setData({
      iscon: !1,
      isshow: !0,
      wallets_password: '',
      ispay: !1,
      iserr: !1,
      isenough: !1,
      isnooilpay: !1,
      iswashpay: !1,
      isshowhis: !1
    })
    that.washmycard()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})